/**
 * Created by charlie on 18-1-17.
 */
import { taskReceived,timeOut,thisDone } from '../../services/taskCenter'
import { Submit } from '../../services/taskReceive'
import {Actions} from 'react-native-router-flux'
import {QueryTask} from '../../services/receiveTask'


function genList(arg){
    let tmp = arg.filter(val => {
        return val.selected === true
    })
    if( tmp.length === 0 ) return []
    let obj = {}
    tmp.map((val, index)=> {
        obj[`userIds[${index}]`] = val.id
    })
    return obj
}
export default {
    namespace:'taskCenter',
    state:{
        tabNum:0,
        page:1,
        pageSize:9,
        list1:[],
        list2:[],
        list3:[],
        list4:[],
        loading:true,
        last:0,
        fun:'getTableList',
        tabChanged:false,
        thisOpen:false,
        userList:[],
        receiveTaskName:'',
        receiveTaskId:''
    },
    subscriptions:{

    },
    effects:{
        *getTableListNoReceive({payload},{call,put,select}){
            yield put({type:'getLoading'});
            const { page, pageSize } = yield select(state => state.taskCenter)
            const { userId } = yield select(state => state.login)

            let current = '';
            if(payload === '' || payload === undefined){
                current = page;
            }else {
                if (payload.page ==='' || payload.page === undefined){
                    current = page;
                }else {
                    current = payload.page;
                }
            }

            const { json, error } = yield call(taskReceived, {userId: userId, receiveStatus: 0,
                pageNo: current, pageSize: pageSize})
            if(error || json.code === '0'){
                yield put({type: 'cleanLoading'})
            }else{
                yield put({type: 'getSuccessNoReceive', payload: json})
            }
        },
        *getTableList({payload},{call,put,select}){
            yield put({type:'getLoading'});
            const { page, pageSize } = yield select(state => state.taskCenter)
            const { userId } = yield select(state => state.login)

            let current = '';
            if(payload === '' || payload === undefined){
                current = page;
            }else {
                if (payload.page ==='' || payload.page === undefined){
                    current = page;
                }else {
                    current = payload.page;
                }
            }

            const { json, error } = yield call(taskReceived, {userId: userId, receiveStatus: 1,
                pageNo: current, pageSize: pageSize})
            if(error || json.code === '0'){
                yield put({type: 'cleanLoading'})
            }else{
                yield put({type: 'getSuccess', payload: json})
            }
        },

        *getTableListTimeOut({payload},{call,put,select}){
            yield put({type:'getLoading'});
            const { page, pageSize } = yield select(state => state.taskCenter)
            const { userId } = yield select(state => state.login)

            let current = '';
            if(payload === '' || payload === undefined){
                current = page;
            }else {
                if (payload.page ==='' || payload.page === undefined){
                    current = page;
                }else {
                    current = payload.page;
                }
            }

            const { json, error } = yield call(timeOut, {userId: userId,pageNo: current, pageSize: pageSize})
            if(error || json.code === '0'){
                yield put({type: 'cleanLoading'})
            }else{
                yield put({type: 'getSuccessTimeOut', payload: json})
            }
        },
        *getTableListDone({payload},{call,put,select}){
            yield put({type:'getLoading'});
            const { page, pageSize } = yield select(state => state.taskCenter)
            const { userId } = yield select(state => state.login)
            let current = '';
            if(payload === '' || payload === undefined){
                current = page;
            }else {
                if (payload.page ==='' || payload.page === undefined){
                    current = page;
                }else {
                    current = payload.page;
                }
            }

            const { json, error } = yield call(thisDone, {userId: userId,pageNo: current, pageSize: pageSize})
            if(error || json.code === '0'){
                yield put({type: 'cleanLoading'})
            }else{
                yield put({type: 'getSuccessDone', payload: json})
            }
        },
        *tabChange({payload},{call,put,select}){
            let fun = '';
            if(payload == 0){
                yield put({type:'getTableListNoReceive'})
                fun = 'getTableListNoReceive';
            }else if(payload == 1){
                yield put({type:'getTableList'})
                fun = 'getTableList';
            }else if(payload == 2){
                yield put({type:'getTableListTimeOut'})
                fun = 'getTableListTimeOut';
            }else {
                yield put({type:'getTableListDone'})
                fun = 'getTableListDone';
            }
            yield put({type:'tabChangeSuccess',payload:{tab:payload,fun:fun}})
        },
        *receiveTask({payload},{call,put,select}){
            let param={
                taskId:payload.taskId,
                taskName:payload.name,
                taskNum:payload.taskNum,
            };
            // yield put({type:'taskReceive/query',payload:param});
            // Actions.push('ReceiveTask', param)
            yield put({type:'query',payload:param})
            // yield put({type:'thisReceive',payload:param})
        },
        *query({payload}, {put, call, select}){
            const { json, error } = yield call(QueryTask, {taskId: payload.taskId})
            if(json.code === '0'){
                console.log('Error: ' + json.message)
            }else{
                // yield put({type: 'getTaskId', payload: payload.taskId})
                yield put({type: 'thisReceive', payload: {data:json.data,param:payload}})
                // Actions.push('ReceiveTask', { ...payload })
            }
        },
        *setTaskNewId({payload},{call,put,select}){
            yield put({type: 'masterTaskList/setTaskId',payload:payload})
        },

      //清楚所有表单数据
      *cleanData({payload}, {call, put, select}){
        yield put({type: 'sign/cleanSignImgs'});
        yield put({type: 'photo/cleanPhotoImgs'});
        yield put({type: 'signIn/cleanData'});
        yield put({type: 'signOut/getClean'});
        yield put({type: 'rectifyBill/resetData'});
      },
        *thisTaskReceive({payload},{call,put,select}){
            const { userList } = yield select(state => state.taskCenter)
            const userIds = genList(userList)
            if( userIds.length === 0 ){
                console.log('Submit Error: ')
            }else{
                const { json, error } = yield call(Submit, {taskId: payload, ...userIds})
              console.log(json,error,'接收')
                if(json.code === '0'){
                    console.log('Submit Error: ' + json.message)
                }else{
                    yield put({type: 'tasksList/queryTasks'});
                    yield put({type:'taskCenter/getTableListNoReceive'})
                    yield put({type:'indexPage/getTableListNoReceive'})
                    yield put({type:'thisModalClose'})
                }
            }
        },

    },
    reducers:{
        thisModalClose(state,{payload}){
            let list = state.userList;
            for(let i in list){
                if(list[i].id === payload){
                    list[i].selected = !list[i].selected;
                }
            }
            return{...state,thisOpen:false}
        },
        thisPersonSelect(state,{payload}){
            let list = state.userList;
            for(let i in list){
                if(list[i].id === payload){
                    list[i].selected = !list[i].selected;
                }
            }
            return{...state,userList:[].concat(list)}
        },
        thisReceive(state,{payload}){
            let list = payload.data.map(val => {
                let tmp = {id: val.userId, name: val.userName, selected: true}
                return tmp
            })
            return{...state,thisOpen:true,receiveTaskName:payload.param.taskName,userList:[].concat(list),receiveTaskId:payload.param.taskId}
        },
        getSuccess(state,{payload}){
            let num = payload.data.pageNo;
            let last = payload.data.last;
            let list = payload.data.list.map(val => {
                let tmp = {
                    id: val.id,
                    signInStatus:val.signInStatus,
                    isChangeForm:val.isChangeForm,
                    signOutStatus:val.signOutStatus,
                    tableData: [
                        val.prjName,
                        val.name,
                        val.createDate.length>10?val.createDate.substring(0,10):val.createDate,
                        val.closingDate.length>10?val.closingDate.substring(0,10):val.closingDate,
                        val.signInDate.length>10?val.signInDate.substring(0,10):val.signInDate,
                    ]
                };
                return tmp
            });
            return {...state,list1:[].concat(list),last:last,page:num,loading:false}
        },
        getSuccessNoReceive(state,{payload}){
            let num = payload.data.pageNo;
            let last = payload.data.last;
            let list = payload.data.list.map(val => {
                let tmp = {
                    id: val.id,
                    receiveStatus:val.receiveStatus,
                    taskNum:val.taskNum,
                    name:val.name,
                    tableData: [
                        val.prjName,
                        val.name,
                        val.createDate.length>10?val.createDate.substring(0,10):val.createDate,
                        val.closingDate.length>10?val.closingDate.substring(0,10):val.closingDate
                    ]
                };
                return tmp
            });
            return {...state,list2:[].concat(list),last:last,page:num,loading:false}
        },
        getSuccessTimeOut(state,{payload}){
            let num = payload.data.pageNo;
            let last = payload.data.last;
            let list = payload.data.list.map(val => {
                let tmp = {
                    id: val.id,
                    receiveStatus:val.receiveStatus,
                    taskNum:val.taskNum,
                    name:val.name,
                    tableData: [
                        val.prjName,
                        val.name,
                        val.createDate.length>10?val.createDate.substring(0,10):val.createDate,
                        val.closingDate.length>10?val.closingDate.substring(0,10):val.closingDate,
                        val.receiveStatus ==='1'?'未签到':'未接收'
                    ]
                };
                return tmp
            });
            return {...state,list3:[].concat(list),last:last,page:num,loading:false}
        },
        getSuccessDone(state,{payload}){
            let num = payload.data.pageNo;
            let last = payload.data.last;
            let list = payload.data.list.map(val => {
                let tmp = {
                    id: val.id,
                    tableData: [
                        val.prjName,
                        val.name,
                        val.createDate.length>10?val.createDate.substring(0,10):val.createDate,
                        val.closingDate.length>10?val.closingDate.substring(0,10):val.closingDate,
                        val.signInDate.length>10?val.signInDate.substring(0,10):val.signInDate,
                    ]
                };
                return tmp
            });
            return {...state,list4:[].concat(list),last:last,page:num,loading:false}
        },
        tabChangeSuccess(state,{payload}){
            return {...state,tabNum:payload.tab,fun:payload.fun,page:1,tabChanged:true}
        },
        getClean(state,{payload}){
            return {...state,tabNum:0,loading:true,page:1,last:0,tabChanged:false}
        },
        getLoading(state,{payload}){
            return{...state,loading:true}
        },
        cleanLoading(state,{payload}){
            return{...state,loading:false}
        },
        thisTaskCancle(state,{payload}){
            return{...state,
                thisOpen:false,
                userList:[],
                receiveTaskName:'',
                receiveTaskId:''
            }
        },
    }
}